package com.ms.auction.customer.controller;


import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ms.auction.common.module.customer.Account;
import com.ms.auction.customer.service.IAccountService;
import com.ms.auction.utils.respons.Result;
import com.ms.auction.utils.respons.ResultBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 账户 前端控制器
 * </p>
 *
 * @author 安能
 * @since 2019-07-10
 */
@Api(value = "/account", tags = "账户Api")
@RestController
@RequestMapping("/account")
public class AccountController {


    @Resource
    IAccountService accountService;


    /**
     * 修改支付密码
     *
     * @param account 账户表
     * @return
     */
    @PostMapping("updatePayPassword")
    public Result updatePayPassword(@RequestHeader("userId") Integer userId, Account account) {

        String md5 = SecureUtil.md5(account.getPassword());
        account.setSubjectId(userId);
        return ResultBuilder.success(
                accountService.update(account, new UpdateWrapper<Account>()
                        .eq("subject_id", account.getSubjectId())
                        .eq("type", account.getType())
                )
        );
    }

    /**
     * 账户余额查询
     *
     * @param account 账户表
     * @return
     */
    @ApiOperation(value = "查询某个用户账户余额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "header"),
            @ApiImplicitParam(name = "subjectId", value = "主题Id", required = true),
            @ApiImplicitParam(name = "type", value = "账户类型 1:客户账户 2:代理商账户 3:组织者账户", required = true)
    })
    @GetMapping("getAccount")
    public Result<Account> getAccount(@RequestHeader("userId") Integer userId,Account account) {

        //参数校验
        if (account == null) {
            return ResultBuilder.errorInfo("参数不全，请确认参数");
        }
        account.setSubjectId(userId);

        account = accountService.getOne(
                new QueryWrapper<Account>()
                        .eq("subject_id", account.getSubjectId())
                        .eq("type", account.getType()
                        )
        );

        return ResultBuilder.success(account);
    }
}
